
* REPLICATION CODE: JENNINGS & WLEZIEN, 'THE COMPARATIVE TIMELINE OF ELECTIONS', AMERICAN JOURNAL OF POLITICAL SCIENCE *

* FIGURE S9. Root Mean Squared Errors (US, UK and Germany), Legislative Elections in Presidential vs. Legislative Systems, Vote = Polls

*********************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - U.S., U.K., & GERMANY - VOTE = POLLS *********************
*********************************************************************************************

use MI_COVAR_ALL.dta, clear

keep if election=="Legislative"
keep if system=="Presidential"

gen rmse=.
gen se_rmse=.

foreach x of numlist 1/200 {
quietly mi estimate, cmdok: mybstrprmseareg vote_ poll_ if daysbeforeED==`x' & ipoll!=. & imp_!=. & iics_!=. 
matrix bts = e(b_mi)
scalar bt = bts[1,1]
replace rmse=bt in `x'
matrix bts = e(V_mi)
scalar bt = bts[1,1]
replace se_rmse=sqrt(bt) in `x'
ereturn clear
}

*********************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *******************************************************
*********************************************************************************************

keep rmse se_rmse
gen daysbeforeED=_n

drop if rmse == .
mi xtset, clear 
sort daysbeforeED

gen rmse_min95=rmse+se_rmse
gen rmse_max95=rmse-se_rmse

save MI_LEG_PRES_RMSE_EQ1.dta, replace

*********************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - U.S., U.K., & GERMANY - VOTE = POLLS *********************
*********************************************************************************************

use MI_COVAR_ALL.dta, clear

keep if election=="Legislative"
keep if system=="Parliamentary"

gen rmse=.
gen se_rmse=.

foreach x of numlist 1/200 {
quietly mi estimate, cmdok: mybstrprmseareg vote_ poll_ if daysbeforeED==`x' & ipoll!=. & imp_!=. & iics_!=. 
matrix bts = e(b_mi)
scalar bt = bts[1,1]
replace rmse=bt in `x'
matrix bts = e(V_mi)
scalar bt = bts[1,1]
replace se_rmse=sqrt(bt) in `x'
ereturn clear
}

*********************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *******************************************************
*********************************************************************************************

keep rmse se_rmse
gen daysbeforeED=_n

drop if rmse == .
mi xtset, clear 
sort daysbeforeED

gen rmse_min95=rmse+se_rmse
gen rmse_max95=rmse-se_rmse

save MI_LEG_PARL_RMSE_EQ1.dta, replace

*******************************************************************************
** LEGISLATIVE ELECTIONS IN PRESIDENTIAL vs. PARLIAMENTARY SYSTEMS ************
*******************************************************************************

use MI_LEG_PARL_RMSE_EQ1.dta, clear
rename rmse leg_parl_eqn1_rmse
rename rmse_min95 leg_parl_eqn1_rmse_min95
rename rmse_max95 leg_parl_eqn1_rmse_max95
drop se_rmse
sort daysbeforeED
save MI_LEG_PARL_RMSE_EQ1.dta, replace

use MI_US_LEG_RMSE_EQ1.dta, clear
rename rmse pres_eqn1_rmse
rename rmse_min95 pres_eqn1_rmse_min95
rename rmse_max95 pres_eqn1_rmse_max95
drop se_rmse
sort daysbeforeED
save MI_LEG_PRES_RMSE_EQ1.dta, replace

use MI_LEG_PRES_RMSE_EQ1.dta, clear
merge daysbeforeED using MI_LEG_PARL_RMSE_EQ1.dta
drop _merge
sort daysbeforeED

* 
twoway /*
*/  || line pres_eqn1_rmse daysbeforeED,                 /*
*/  clpattern(solid) clcolor(black) clwidth(medthick)    /* 
*/  || line leg_parl_eqn1_rmse daysbeforeED,             /*
*/  clpattern(dash) clcolor(black) clwidth(medthick)     /* 
*/  || line pres_eqn1_rmse_min95 daysbeforeED,           /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line pres_eqn1_rmse_max95 daysbeforeED,           /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line leg_parl_eqn1_rmse_min95 daysbeforeED,       /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line leg_parl_eqn1_rmse_max95 daysbeforeED,       /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  , scheme(s2mono) graphregion(color(white))           /*
*/  xtitle("Days Until Election")                        /*
*/  title("")                                            /*
*/  ylabel(0(1)8, gmax angle(horizontal))                /*
*/  ytick(0(1)8)                                         /*
*/  ytitle(Root Mean Squared Error)                      /*
*/  xlabel(0(50)200)                                     /*
*/  xscale(reverse) xscale(titlegap(2))                  /*
*/  legend(order(1 "Presidential" 2 "Parliamentary")     /*
*/  rows(1)                                              /*
*/  size(medsmall))

graph export FigS9.tif
